<template>
    <div class="container">
        <el-form inline ref="search" :model="searchForm" label-width="80px">
            <el-form-item label="学号">
                <el-input v-model="searchForm.userId" placeholder="请输入学号"></el-input>
            </el-form-item>
            <el-form-item label="姓名">
                <el-input v-model="searchForm.name" placeholder="请输入姓名"></el-input>
            </el-form-item>
            <el-form-item label="年龄">
                <el-input v-model="searchForm.age" placeholder="请输入年龄"></el-input>
            </el-form-item>
            <el-form-item label="年级">
                <el-input v-model="searchForm.grade" placeholder="请输入年级"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="getData">查询</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="tableData" style="width: 100%" empty-text="暂无更多数据" @selection-change="selectionChange">
            <el-table-column :selectable="selectable" prop="selection" type="selection" width="55"></el-table-column>
            <el-table-column prop="id" label="学生ID" width="80"></el-table-column>
            <el-table-column label="头像" width="80">
                <template #default="scope">
                    <el-avatar :size="50" :src="scope.row.image" @error="errorHandler">
                        <img src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" />
                    </el-avatar>
                </template>
            </el-table-column>
            <el-table-column prop="userId" label="学号" width="80"></el-table-column>
            <el-table-column prop="name" label="姓名" width="80"></el-table-column>
            <el-table-column prop="age" label="年龄" width="80"></el-table-column>
            <el-table-column prop="phone" label="手机号" width="120"></el-table-column>
            <el-table-column prop="gender" label="性别" width="80"></el-table-column>
            <el-table-column prop="grade" label="年级" width="80"></el-table-column>
            <el-table-column prop="password" label="密码" width="120"></el-table-column>
            <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
            <el-table-column prop="updateTime" label="更新时间" width="180"></el-table-column>
            <el-table-column fixed="right" min-width="120" label="操作">
                <template #default="scope">
                    <el-button type="primary" link size="small"
                        @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="primary" link size="small"
                        @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog v-model="dialogFormVisible" title="编辑学生信息" width="500">
            <el-form :model="form">
                <el-form-item label="学生姓名" label-width="140px">
                    <el-input v-model="form.name" placeholder="请输入姓名" autocomplete="off" />
                </el-form-item>
                <el-form-item label="学生学号" label-width="140px">
                    <el-input v-model="form.userId" placeholder="请输入学号" autocomplete="off" />
                </el-form-item>
                <el-form-item label="学生年龄" label-width="140px">
                    <el-input v-model="form.age" placeholder="请输入年龄" autocomplete="off" />
                </el-form-item>
                <el-form-item label="学生年级" label-width="140px">
                    <el-input v-model="form.grade" placeholder="请输入年级" autocomplete="off" />
                </el-form-item>
                <el-form-item label="学生手机号" label-width="140px">
                    <el-input v-model="form.phone" placeholder="请输入学生手机号" autocomplete="off" />
                </el-form-item>
                <el-form-item label="密码" label-width="140px">
                    <el-input v-model="form.password" placeholder="请输入密码" autocomplete="off" />
                </el-form-item>
                <el-form-item label="性别" label-width="140px">
                    <el-select v-model="form.gender" placeholder="请选择性别">
                        <el-option label="男" value="男" />
                        <el-option label="女" value="女" />
                    </el-select>
                </el-form-item>
                <el-form-item label="学生头像" label-width="140px">
                    <template #default="scope">
                        <el-upload class="avatar-uploader" name="avatar" action="http://127.0.0.1:8080/ali/upload"
                            :show-file-list="false" :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload">
                            <img v-if="form.image && form.image != ''" style="width: 88px; height: 78px;"
                                class="avatar-uploader-icon" :src="form.image && form.image" />
                            <el-icon v-else class="avatar-uploader-icon">
                                <Plus />
                            </el-icon>
                        </el-upload>
                    </template>

                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取消</el-button>
                    <el-button type="primary" @click="dialogFuction">
                        确定
                    </el-button>
                </div>
            </template>
        </el-dialog>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
            :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
    </div>
</template>

<script setup>
import { reactive, onMounted, ref } from 'vue';
import { getUsers, updateUser, deleteUsers } from '@/api/table/index.js';
import { ElMessage, ElNotification } from 'element-plus';
import { el } from 'element-plus/es/locales.mjs';
const dialogFormVisible = ref(false);
const form = ref();
const getData = async () => {
    const data = await getUsers(searchForm);
    console.log(data);

    if (data.code == 200) {
        data.data.forEach(item => {
            item.selection = false;
        })
        tableData.value = data.data;
    } else {
        ElNotification.error({
            title: '获取学生信息',
            message: data.msg,
            showClose: false,
            duration: 1000
        })
    }
}

onMounted(async () => {
getData();
})

const selectable = ref();
const searchForm = reactive({
    userId: null,
    name: null,
    age: null,
    grade: null
})
var tableData = ref() // 表格数据
const currentPage = ref(1) // 当前页码
const pageSize = ref(10) // 每页显示条数
const total = ref(0)// 总条数

function handleEdit(index, row) {
    form.value = row;
    dialogFormVisible.value = !dialogFormVisible.value;

}
async function handleDelete(index, row) {
    let str = ''
    selectable.value.forEach(item => {
        if (str == '') {
            str = item.id
        } else {
            str = str + ',' + item.id
        }
    })
    const data = await deleteUsers(str.length > 0 ? str : JSON.stringify(row.id));
    if (data.code == 200) {
        ElNotification.success({
            title: '删除学生',
            message: data.msg,
            showClose: false,
            duration: 1000
        })
    } else {
        ElNotification.error({
            title: '删除学生',
            message: data.msg,
            showClose: false,
            duration: 1000
        })
    }

    getData();
}
function handleSizeChange(val) {
    // 改变每页显示条数
    this.pageSize = val;
}
function handleCurrentChange(val) {
    // 改变当前页码
    this.currentPage = val;
}

const dialogFuction = async () => {
    const data = await updateUser(form.value);
    if (data.code == 200) {
        dialogFormVisible.value = false;
        ElNotification.success({
            title: '修改学生信息',
            message: data.msg,
            showClose: false,
            duration: 1000
        })
        form.value = {};

    } else {
        ElNotification.error({
            title: '修改学生信息',
            message: data.msg,
            showClose: false,
            duration: 1000
        })
    }
}
const handleAvatarSuccess = (
    response
) => {
    form.value.image = response.data.url;
    ElNotification.success({
        title: '头像上传',
        message: response.msg,
        showClose: false,
        duration: 1000
    })
}
const selectionChange = (row) => {
    selectable.value = row;
}
const beforeAvatarUpload = (rawFile) => {
    if (rawFile.type !== 'image/jpeg') {
        ElMessage.error('Avatar picture must be JPG format!')
        return false
    } else if (rawFile.size / 1024 / 1024 > 2) {
        ElMessage.error('Avatar picture size can not exceed 2MB!')
        return false
    }
    return true
}
</script>

<style scoped lang="less">
.container {
    width: 80%;
    margin: 20px auto;

    .el-icon.avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        border: 1px dashed var(--el-border-color);
        width: 88px;
        height: 78px;
        text-align: center;
    }
}
</style>